package com.example.home.view;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.common.control.MyBindingAdapter;
import com.example.home.CollectionAdapter;
import com.example.home.viewmodel.HomeViewModel;
import com.example.home.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.jetbrains.annotations.NotNull;

public class HomeFragment extends Fragment {

    private static final String TAG = "HomeTestFragment";
    private HomeViewModel mViewModel;


    private CollectionAdapter collectionAdapter;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.home_fragment, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // todo: viewpager
        collectionAdapter=new CollectionAdapter(this);
        ViewPager2 viewPager2 = view.findViewById(R.id.home_pager);
        viewPager2.setAdapter(collectionAdapter);
        //TabLayout绑定
        TabLayout tabLayout=view.findViewById(R.id.home_pager_tab);
        new TabLayoutMediator(tabLayout, viewPager2,(tab, position) -> {
            tab.setText(collectionAdapter.getFragmentName(position));
        }).attach();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);

        // TODO: Use the ViewModel
    }

}